package com.the_ring.chapter15.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

public class PageView extends FrameLayout {

    private boolean isUpToTop = false;      // 是否高亮显示

    public PageView(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (isUpToTop) {
            // 最上一页
            // 给画布涂上透明颜色，去掉遮罩
            canvas.drawColor(Color.TRANSPARENT);
        } else {
            // 不是最上一层
            // 给画布涂上半透明颜色，加上遮罩
            canvas.drawColor(0x55000000);
        }
    }

    // 设置是否高亮显示
    public void setUp(boolean isUp) {
        isUpToTop = isUp;
        postInvalidate();
    }

    public void setMargin(int margin) {
        // 获取空白边缘的布局参数
        MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
        params.leftMargin = margin;
        setLayoutParams(params);
        postInvalidate();
    }
}
